package _1_simple_factory

import "strings"

//RuleConfig 规则配置
type RuleConfig struct {
	fileType string
}

func (r *RuleConfig) String() string {
	return r.fileType
}

//FileParse 文件解析接口
type FileParse interface {
	parse(file string) RuleConfig
}

// jsonFileParse 解析json文件
type jsonFileParse struct {
}

func (j *jsonFileParse) parse(file string) RuleConfig {
	//parse json context
	return RuleConfig{fileType: "json"}
}

// xmlFileParse 解析xml文件
type xmlFileParse struct {
}

func (x *xmlFileParse) parse(file string) RuleConfig {
	//parse xml context
	return RuleConfig{fileType: "xml"}
}

// propertiesFileParse 解析 properties 文件
type propertiesFileParse struct {
}

func (p *propertiesFileParse) parse(file string) RuleConfig {
	//parse properties context
	return RuleConfig{fileType: "properties"}
}

// GetFileParse 简单工厂方法实现
// 最简工厂方法
func GetFileParse(filePath string) FileParse {
	if strings.Contains(filePath, ".json") {
		return &jsonFileParse{}
	}
	if strings.Contains(filePath, ".xml") {
		return &xmlFileParse{}
	}
	if strings.Contains(filePath, ".properties") {
		return &propertiesFileParse{}
	}
	panic("not support file :" + filePath)
}

// FileParseFactory 简单工厂接口实现
// 场景：解决工厂创建比较复杂的场景
type FileParseFactory interface {
	createFileParse() FileParse
}

// JsonFileParseFactory jsonFileParse工厂
type JsonFileParseFactory struct {
}

func (j *JsonFileParseFactory) createFileParse() FileParse {
	return &jsonFileParse{}
}

// XmlFileParseFactory xmlFileParse工厂
type XmlFileParseFactory struct {
}

func (j *XmlFileParseFactory) createFileParse() FileParse {
	return &xmlFileParse{}
}

// PropertiesFileParseFactory propertiesFileParse工厂
type PropertiesFileParseFactory struct {
}

func (j *PropertiesFileParseFactory) createFileParse() FileParse {
	return &propertiesFileParse{}
}
